 package imaa.escola.disciplina.service;

import imaa.escola.disciplina.datatransfer.DisciplinaDto;
import imaa.escola.disciplina.datatransfer.PesquisaDisciplinaDto;
import imaa.escola.disciplina.domain.Disciplina;
import imaa.escola.disciplina.repository.DisciplinaRepository;
import imaa.escola.instrumento.repository.InstrumentoRepository;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/disciplina")
public class DisciplinaService {
	
	private static final Logger logger = Logger.getLogger(DisciplinaService.class);
	
	
	@Autowired
	DisciplinaRepository disciplinaRepository;

	@Autowired
	private InstrumentoRepository instrumentoRepository;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody DisciplinaDto inserirDisciplina(@RequestBody DisciplinaDto dto) {
		
		Disciplina disciplina = dto.convertToEntity();
		
		disciplina.setInstrumento(instrumentoRepository.findOne(dto.instrumento.id));
		
		DisciplinaDto result = new DisciplinaDto(disciplinaRepository.save(disciplina));
		
		logger.info("Disciplina inserida na base de dados. ID: " + result.id);
		
		return result;
	}
	@ResponseBody
	@RequestMapping(value = "/get/{idDisciplina}", method = RequestMethod.GET, produces = "application/json")
	public DisciplinaDto getDisciplina(@PathVariable Long idDisciplina) {
		return new DisciplinaDto(disciplinaRepository.findOne(idDisciplina));
	}
	
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	public List<DisciplinaDto> listarDisciplina() {
		List<Disciplina> lista = disciplinaRepository.findAll();
		List<DisciplinaDto> listaDto = new ArrayList<DisciplinaDto>();
		for (Disciplina disciplina : lista) {
			DisciplinaDto dto = new DisciplinaDto(disciplina);
			listaDto.add(dto);
		}
		return listaDto;
	}	
	
	@ResponseBody
	@RequestMapping(value = "/search", method = RequestMethod.GET, produces = "application/json")
	public List<DisciplinaDto> pesquisarDisciplinas(@RequestParam(value = "descricao") String dto) {
		List<Disciplina> lista = disciplinaRepository.findByName(dto);
		List<DisciplinaDto> listaDto = new ArrayList<DisciplinaDto>();
		for (Disciplina disciplina : lista) {
			DisciplinaDto disciplinaDto = new DisciplinaDto(disciplina);
			listaDto.add(disciplinaDto);
		}
		return listaDto;
	}
	
	@RequestMapping(value = "/advancedSearch", method = RequestMethod.POST)
	public @ResponseBody List<DisciplinaDto> pesquisaAvancada(@RequestBody PesquisaDisciplinaDto dto) {
		List<Disciplina> lista = disciplinaRepository.findByExample(dto);
		
		List<DisciplinaDto> listaDto = new ArrayList<DisciplinaDto>();
		for (Disciplina disciplina : lista) {
			DisciplinaDto disciplinaDto = new DisciplinaDto(disciplina);
			listaDto.add(disciplinaDto);
		}
		return listaDto;
	}
	@ResponseBody
	@RequestMapping(value = "/remove/{idDisciplina}", method = RequestMethod.GET, produces = "application/json")
	public void removeDisciplina(@PathVariable Long idDisciplina) {
		disciplinaRepository.delete(idDisciplina);
	}
}
